import axios from 'axios';
import { Message,Loading} from 'element-ui'


const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    // easy-mock服务挂了，暂时不使用了
    baseURL: 'https://localhost:5001/api/',
    timeout: 5000,
    headers: {
        'Content-Type': 'application/json;charset=UTF-8'
    }
});

service.interceptors.request.use(
    config => {
        let token = localStorage.getItem('ms_token');
        config.headers.common["Token"]=token;
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

service.interceptors.response.use(
    response => {     
         
        if (response.status === 200) {
            return response.data;
        } else {
            Promise.reject();
        }
    },
    error => {
        
    if(error&&error.response){
        switch (error.response.status) {
            case 400:
            Message.error({ message: error.response.data });
            break;

            // case 401: err.message = '未授权，请重新登录(401)'; break;
            // case 403: err.message = '拒绝访问(403)'; break;
            // case 404: err.message = '请求出错(404)'; break;
            // case 408: err.message = '请求超时(408)'; break;
            // case 500: err.message = '服务器错误(500)'; break;
            // case 501: err.message = '服务未实现(501)'; break;
            // case 502: err.message = '网络错误(502)'; break;
            // case 503: err.message = '服务不可用(503)'; break;
            // case 504: err.message = '网络超时(504)'; break;
            // case 505: err.message = 'HTTP版本不受支持(505)'; break;
            default:
                Message.error({ message: error.response });
                break;
        }
    }
       
        console.log(error);
        return Promise.reject();
    }
);

export default service;
